package com.letv.core.parser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.letv.core.bean.WaterMarkBean;

/**
 * 请求水印
 * 
 * @author zhuqiao
 *
 */
public class WaterMarkParser extends LetvNormalParser<WaterMarkBean> {
    // 一级节点
    private static final String CALLBACK = "header";
    // private static final String DATA = "data";
    // private static final String MSGS = "msgs";
    private static final String BODY = "body";
    private static final String RESULT = "result";
    // data下节点
    private static final String CID = "cid";
    private static final String IMGS = "data";
    private static final String OFFSET = "offset";
    private static final String PID = "pid";
    // imgs下节点
    private static final String LASTTIME = "lasttime";
    private static final String LINK = "link";
    private static final String POSITION = "position";
    private static final String URL100 = "url";

    public String getRealString(String jsonName) {
        return jsonName.trim();
    }

    @Override
    protected WaterMarkBean parse(String string) throws JSONException {
        JSONObject data = new JSONObject(string);
        
        WaterMarkBean waterMark = new WaterMarkBean();
        waterMark.setCALLBACK(data.get(getRealString(CALLBACK)) + "");
        data = data.getJSONObject(BODY);
        JSONObject object = getJSONObject(data, RESULT);
        if (object != null) {
            waterMark.setOFFSET(object.get(getRealString(OFFSET)) + "");
            JSONArray array = object.getJSONArray(getRealString(IMGS));
            if (array == null) {
                return waterMark;
            }

            for (int i = 0; i < array.length(); i++) {
                WaterMarkBean.Imgs imgs = waterMark.new Imgs();
                JSONObject obj = array.getJSONObject(i);
                imgs.setLASTTIME(obj.get(getRealString(LASTTIME)) + "");
                imgs.setLINK(obj.get(getRealString(LINK)) + "");
                imgs.setPOSITION(obj.get(getRealString(POSITION)) + "");
                imgs.setURL100(obj.get(getRealString(URL100)) + "");
                waterMark.getImgses().add(imgs);
                imgs = null;
            }
            waterMark.setCID(object.get(getRealString(CID)) + "");
            waterMark.setPID(object.get(getRealString(PID)) + "");
        }
        return waterMark;
    }

}
